<template>
  <view>
    <view class="search-box">
      <!-- @confirm="search" -->
      <uni-search-bar v-model="kw" :focus="true" @input="input" cancelButton="none" :radius="100">
      </uni-search-bar>
    </view>

    <!-- 搜索建议列表 -->
    <view style="background-color: #fff;" class="sugg-list" v-if="searchResult.length">
      <view class="sugg-item" v-for="(item,i) in searchResult" :key="i" @click="gotoSearch(item)">
        <view class="goods-name">
          {{item.goods_name}}
        </view>
        <uni-icons type="arrowright" size="16"></uni-icons>
      </view>
    </view>

    <!-- 搜索历史 -->
    <view class="hisotry-box" v-else>
      <!-- 标题区域 -->
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash" size="17" @click="cleanHistory"></uni-icons>
      </view>
      <!-- 列表区域 -->
      <view class="history-list">
        <uni-tag :text="item" class="history-item" v-for="(item,i) in historyList" :key="i" @click="updateHistory(item)"
          custom-style="color:#000;background-color: #efefef;border-color:#efefef;"></uni-tag>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        timer: null, // 延时器的timerId
        kw: '', // 搜索关键词
        searchResult: [], // 搜索结果
        historyList: [], // 搜索历史
      };
    },
    onLoad(){
      // 从本地存储获取搜索关键词
      this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
    },
    methods: {
      // input输入事件的处理函数
      input(e) {
        clearTimeout(this.timer)
        this.timer = setTimeout(() => {
          // this.kw = e
          this.getSearchList()
        }, 500)
      },
      async getSearchList() {
        // 判断关键词是否为空
        if (this.kw.length === 0) {
          this.searchResult = []
          return
        }
        const {data: res} = await uni.$http.get('/api/public/v1/goods/qsearch', {query: this.kw})
        if (res.meta.status !== 200) return uni.$showMsg()
        this.searchResult = res.message
        // 调用保存关键词的方法
        this.saveSearchHistory()
      },
      gotoSearch(item) {
        uni.navigateTo({
          url: "/subpkg/goods_detail/goods_detail?goods_id=" + item.goods_id
        })
      },
      
      // 保存关键词的方法
      saveSearchHistory(){
        const a = this.historyList.indexOf(this.kw)
        if(a !== -1){
          this.historyList.splice(a,1)
        }
        this.historyList.unshift(this.kw)
        // 本地存储搜索关键词，持久化数据
        uni.setStorageSync('kw',JSON.stringify(this.historyList))
      },
      // // 记录搜索历史
      // search(e) {
      //   const res = e.value
      //   const i = this.historyList.indexOf(res)
      //   if (i !== -1) {
      //     this.historyList.splice(i, 1)
      //   }
      //   this.historyList.unshift(res)
      // },
      
      // 清除搜索历史
      cleanHistory(){
        this.historyList = []
        uni.setStorageSync("kw",'[]')
      },
      // 点击搜索历史事件
      updateHistory(item){
        this.kw = item
      }
    }
  }
</script>

<style lang="scss">
  .search-box {
    background-color: #c00000;
    position: sticky;
    top: 0;
    z-index: 999;
  }

  .sugg-list {
    padding: 0 5px;

    .sugg-item {
      font-size: 12px;
      padding: 13px 0;
      display: flex;
      align-items: center;
      justify-content: space-between;
      border-bottom: 1px solid #efefef;

      .goods-name {
        // 文字不允许换行(单行文本)
        white-space: nowrap;
        // 溢出部分隐藏
        overflow: hidden;
        // 文本溢出后,使用...代替
        text-overflow: ellipsis;
        margin-right: 3px;
      }
    }
  }

  .hisotry-box {
    padding: 0 5px;

    .history-title {
      display: flex;
      justify-content: space-between;
      border-bottom: 1px solid #efefef;
      height: 40px;
      align-items: center;
      font-size: 13px;
    }

    .history-list {
      display: flex;
      flex-wrap: wrap;

      .history-item {
        display: flex;
        margin-top: 5px;
        margin-right: 5px;
      }
    }
  }
</style>
